{% extends 'base.html' %}

{% block content %}
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>商品列表</h2>
        {% if user.is_staff %}
        <a href="{% url 'add_product' %}" class="btn btn-primary">添加商品</a>
        {% endif %}
    </div>

    <div class="row">
        {% for product in products %}
        <div class="col-md-4 mb-4">
            <div class="card">
                {% if product.image %}
                <img src="{{ product.image.url }}" class="card-img-top" alt="{{ product.name }}">
                {% endif %}
                <div class="card-body">
                    <h5 class="card-title">{{ product.name }}</h5>
                    <p class="card-text">{{ product.description }}</p>
                    <p class="card-text">价格: ¥{{ product.price }}</p>
                    <p class="card-text">库存: {{ product.stock }}</p>
                    <form action="{% url 'add_to_cart' product.id %}" method="post">
                        {% csrf_token %}
                        <button type="submit" class="btn btn-primary">加入购物车</button>
                    </form>
                </div>
            </div>
        </div>
        {% endfor %}
    </div>
</div>
{% endblock %}